var orderDetailApp = angular.module("orderDetailApp",[]);
orderDetailApp.controller("orderDetailController",function($scope,$http,scCodeService,$location,$state,$stateParams){
	//订单价格数据处理，从2处理成2.00格式
	$scope.intToFloat=function(num){
		num=Math.round(num*100)/100;
		var obj=num.toString().split(".");
		if(obj.length==1){
			num=num+'.00';
		}else{
			if(obj[1].length==1){
				num=num+'0';
			}
		}
		return num;
		
	}
	//订单数据显示处理方法
	$scope.orderShow=function(data){
		//判断订单状态，未支付则停留在此页，已支付则跳转至订单完成界面
		/*if(data.status=="7"){
			$location.path("/orderDetail/"+data.chargeDetail.orderNo);
		}*/
		//显示支付金额
		$scope.isPay=true;
		$scope.notPay=false;
		if(data.payType=="免收费"){
			$scope.isPay=false;
			$scope.notPay=true;
		}
		//计算充电费和服务费，优惠
		$scope.chargePrice=0;
		$scope.servicePrice=0;
		$scope.discountPrice=0;
		$scope.chargeList=data.orderDetails;
		
		angular.forEach($scope.chargeList, function(data,index,array){
			if(array[index].serviceNo=="100002"){
				
				$scope.chargePrice=parseFloat($scope.chargePrice+array[index].totalPrice);
			}else if(array[index].serviceNo=="100001"){
				
				$scope.servicePrice=parseFloat($scope.servicePrice+array[index].totalPrice);
			}
			$scope.discountPrice=parseFloat($scope.discountPrice)+parseFloat(array[index].totalPrice-array[index].realTotalPrice);
		});
		$scope.chargePrice=$scope.intToFloat($scope.chargePrice);
		$scope.servicePrice=$scope.intToFloat($scope.servicePrice);
		//判断优惠金额是否有值，有则显示，无则隐藏
		$scope.hasDiscount=false;
		if($scope.discountPrice && $scope.discountPrice>0){
			$scope.hasDiscount=true;
			$scope.discountPrice=$scope.intToFloat($scope.discountPrice);
		}
		
		//将订单数据存储到本地localStorage里
		var ls=localStorage;
	    //0
	    ls.removeItem('chargeDetail');
	    //清除存入数据
	    ls.setItem('userinfo',JSON.stringify($scope.userinfo));
	    ls.setItem('chargeDetail',JSON.stringify(data));
	    //显示存入数据
	}
	//获取路由上的订单id调用接口获取订单详情
	$scope.getOrderDetailByOrderNo=function(){
		//获取路由上获取订单id
		if($stateParams.orderNo){
			$scope.orderNo=$stateParams.orderNo;
		}
		
		var postData = {
			orderNo:$scope.orderNo
		}
		scCodeService.getOrderDetail(postData).then(function(data){
				
				if(data.code == "200"){
					 if(data.data){
						$scope.order=data.data;
						
						//调用订单显示数据处理方法
						$scope.orderShow($scope.order);
					}
				}
			})
	}
	$scope.getOrderDetailByOrderNo();
})